package util;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class sendMail {
	
 private MimeMessage mimeMsg;  //MIME�ʼ�����

 private Session session;      //�ʼ��Ự����
 private Properties props;     //ϵͳ����
 private boolean needAuth = false;  //smtp�Ƿ���Ҫ��֤

 private String username = "";  //smtp��֤�û��������
 private String password = "";

 private Multipart mp;    //Multipart����,�ʼ�����,����,���������ݾ���ӵ����к������MimeMessage����


/**
 * 
 */
 public sendMail() 
 {
   //setSmtpHost(getConfig.mailHost);//���û��ָ���ʼ�������,�ʹ�getConfig���л�ȡ
	 setSmtpHost("smtp.qq.com");
	 createMimeMessage();
 }

 public sendMail(String smtp){
   setSmtpHost(smtp);
   createMimeMessage();
 }


/**
 * @param hostName String
 */
 public void setSmtpHost(String hostName) 
 {
   System.out.println("����ϵͳ���ԣ�mail.smtp.host = "+hostName);
   System.out.println(props) ;
   if(props == null)props = System.getProperties();         //���ϵͳ���Զ���
   System.out.println(props) ;
   props.put("mail.smtp.host",hostName);                 //����SMTP����
   System.out.println(hostName) ;
 }


/**
 * @return boolean
 */
 public boolean createMimeMessage()
 {
   try{
     System.out.println("׼����ȡ�ʼ��Ự����");
     session = Session.getDefaultInstance(props,null);   //����ʼ��Ự����
   }
   catch(Exception e){
     System.err.println("��ȡ�ʼ��Ự����ʱ�������"+e);
     return false;
   }

   System.out.println("׼������MIME�ʼ�����");
   try{
     mimeMsg = new MimeMessage(session);                 //����MIME�ʼ�����
     mp = new MimeMultipart();

     return true;
   }
   catch(Exception e){
     System.err.println("����MIME�ʼ�����ʧ�ܣ�"+e);
     return false;
   }
 }


/**
 * @param need boolean
 */
 public void setNeedAuth(boolean need) {
   System.out.println("����smtp�����֤��mail.smtp.auth = "+need);
   if(props == null)props = System.getProperties();

   if(need){
     props.put("mail.smtp.auth","true");
   }else{
     props.put("mail.smtp.auth","false");
   }
 }


/**
 * @param name String
 * @param pass String
 */
 public void setNamePass(String name,String pass) {
   username = name;
   password = pass;
 }


/**
 * @param mailSubject String
 * @return boolean
 */
 public boolean setSubject(String mailSubject) {
   System.out.println("�����ʼ����⣡");
   try{
     mimeMsg.setSubject(mailSubject);
     return true;
   }
   catch(Exception e) {
     System.err.println("�����ʼ����ⷢ�����");
     return false;
   }
 }



/**
 * @param mailBody String
 */
 public boolean setBody(String mailBody) {
   try{
     BodyPart bp = new MimeBodyPart();
     bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
     mp.addBodyPart(bp);

     return true;
   }
   catch(Exception e){
     System.err.println("�����ʼ�����ʱ�������"+e);
     return false;
   }
 }


/**
 * @param name String
 * @param pass String
 */
 public boolean addFileAffix(String filename) {

System.out.println("�����ʼ�������"+filename);

try{
     BodyPart bp = new MimeBodyPart();
     FileDataSource fileds = new FileDataSource(filename);
     bp.setDataHandler(new DataHandler(fileds));
     bp.setFileName(fileds.getName());

     mp.addBodyPart(bp);

     return true;
   }
   catch(Exception e){
     System.err.println("�����ʼ�������"+filename+"�������"+e);
     return false;
   }
 }


/**
 * @param name String
 * @param pass String
 */
 public boolean setFrom(String from) {
   System.out.println("���÷����ˣ�");
   try{
     mimeMsg.setFrom(new InternetAddress(from));             //���÷�����
     return true;
   }
   catch(Exception e)
   { return false; }
 }


/**
 * @param name String
 * @param pass String
 */
 public boolean setTo(String to){
   if(to == null)return false;

   try{
     mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
     return true;
   }
   catch(Exception e)
   {   return false;   }

 }

/**
 * @param name String
 * @param pass String
 */
 public boolean setCopyTo(String copyto)
 {
   if(copyto == null)return false;
   try{
     mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
     return true;
   }
   catch(Exception e)
   { return false; }
 }


/**
 * @param name String
 * @param pass String
 */
 public boolean sendout()
 {
   try{
     mimeMsg.setContent(mp);
     mimeMsg.saveChanges();
     System.out.println("���ڷ����ʼ�....");

     Session mailSession = Session.getInstance(props,null);
     Transport transport = mailSession.getTransport("smtp");
     transport.connect((String)props.get("mail.smtp.host"),username,password);
     transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
     //transport.send(mimeMsg);

     System.out.println("�����ʼ��ɹ���");
     transport.close();

     return true;
   }
   catch(Exception e)
   {
     System.err.println("�ʼ�����ʧ�ܣ�"+e);
     return false;
   }
 }


 /*
  *�����ʼ�ȷ�����ӵ����   http://localhost:8080/Mail/
  *http://localhost:8080/TestWeb/index.jsp?currentRecord=12&pageSize=6
  */
 public String ConstructURL (String userName,String rnd)
 {
	 String URL = new String("http://localhost:8080/user/verify.do?userName="+userName+"&rnd="+rnd) ;
	 return URL ;
 }
 
 public void sendmail(String sendTo,String userName,String rnd )
 {
	   sendMail themail = new sendMail("smtp.qq.com");
	   
	   String URL = themail.ConstructURL(userName,rnd) ;
	   String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
	   "<div align=center><a href="+URL+">"+URL+"</a></div>";
	   
	   themail.setNeedAuth(true);

	   System.out.println("fdsafdsafd") ;
	   
	   if(themail.setSubject("����") == false) return;
	   if(themail.setBody(mailbody) == false) return;
	   if(themail.setTo(sendTo) == false) return;
	   if(themail.setFrom("570177747@qq.com") == false) return;
	   themail.setNamePass("570177747@qq.com","ilzengali");

	   if(themail.sendout() == false) 
	   {
		   System.out.println("���ʹ�����");
		   return;
	   }
 }
 /**
  *  Just do it as this
  */
 public static void main(String[] args) {
  
	 String a = "adfasdfasdf" ;
	 String b = "fdsafdsafdsa" ;
   
	// System.out.println("good");
   sendMail themail = new sendMail("smtp.qq.com");
   
   
   String URL = themail.ConstructURL(a,b) ;
   String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
   "<div align=center><a href="+URL+">"+URL+"</a></div>";
   
   themail.setNeedAuth(true);

   System.out.println("fdsafdsafd") ;
   
   if(themail.setSubject("激活") == false) return;
   if(themail.setBody(mailbody) == false) return;
   if(themail.setTo("rackingroll@163.com") == false) return;
   if(themail.setFrom("570177747@qq.com") == false) return;
//   if(themail.addFileAffix("c:\\boot.ini") == false) return;
   themail.setNamePass("570177747@qq.com","ilzengali");

   if(themail.sendout() == false) 
   {
	   System.out.println("���ʹ�����");
	   return;
   }
 }
}
